<?php
/**
 */
// from 0 local 1 from_mblog

define("CACHE_PREFIX_PUBU","_pubu_content");

class db_mblog_video_content extends db_base {
    
	var $table_name = "mblog_video_content";
	
	function db_mblog_video_content()
	{
		parent::db_base();
	}
	
	

	function get_mblog_video_content_item($id)
	{
		$mc = mem_cache::get_instance();
	
		$key = CACHE_PREFIX_video."-get_mblog_video_content_item-{$id}";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			$id = intval($id);
			$where_ary = array();
			$where_ary[] = "id = $id";
			$where_ary[] = "status = 1";
			$where = join (" and ",$where_ary);
			
			$content = $this->get_alllist($this->table_name,1,1,"",$where);
			
			$content[0]["content_no_url"] =  preg_replace("/http[\S]+/","",$content[0]["content"]);
			
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content[0];
		
	}
	
	function get_mblog_video_content_have_pic($p,$pcount,$topic_id)
	{
		$mc = mem_cache::get_instance();
	
		$key = CACHE_PREFIX_video."-get_mblog_video_content_have_pic-{$topic_id}";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			$id = intval($id);
			$where_ary = array();
			if($topic_id)$where_ary[] = "topic_id = $topic_id";
			$where_ary[] = "pic != '' ";
			$where_ary[] = "status = 1";
			$where = join (" and ",$where_ary);
			$content = $this->get_alllist($this->table_name,$p,$pcount,"",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
		
	}
	

	function get_mblog_video_content_num_top($pcount=5,$topic_id)
	{
		$mc = mem_cache::get_instance();
	
		$key = CACHE_PREFIX_video."-get_mblog_video_content_num_top-{$topic_id}-{$pcount}";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			$id = intval($id);
			$where_ary = array();
			if($topic_id)$where_ary[] = "topic_id = $topic_id";
			$where_ary[] = "status = 1";
			$where = join (" and ",$where_ary);
			$sql ="select count(*) as count ,create_uid,create_nickname from {$this->table_name} where {$where} group by create_uid order by count desc limit $pcount";
			
			$content = $this->select($sql);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
		
	}
	
	
	
	function get_mblog_video_content_item_with_del($id)
	{
		$mc = mem_cache::get_instance();
	
		$key = CACHE_PREFIX_video."-get_mblog_video_content_item_with_del-{$id}";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			$id = intval($id);
			$where_ary = array();
			$where_ary[] = "id = $id";
			$where = join (" and ",$where_ary);
			
			$content = $this->get_alllist($this->table_name,1,1,"",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content[0];
		
	}
	
	function get_mblog_video_count_by_date($show_type="",$date="")
	{
		$mc = mem_cache::get_instance();
	
		$key = CACHE_PREFIX_video."-get_mblog_video_count_by_date-{$show_type}-{$date}";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			$show_type = mysql_escape_string($show_type);
			$date = mysql_escape_string($date);
			
			$where_ary = array();
			if(strlen($show_type)>0)$where_ary[] = "show_type = $show_type";
			if(strlen($date)>0)$where_ary[] = "update_date = '{$date}'";
			$where_ary[] = "status = 1";
			$where = join (" and ",$where_ary);
			
			$content = $this->get_listcount($this->table_name,$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
		
	}

	
	function get_mblog_video_list_by_topic($p=1,$pcount=10,$topic_id="",$uid="")
	{
		
		$mc = mem_cache::get_instance();
	
		$key = CACHE_PREFIX_video."-get_mblog_video_list_by_topic-{$p}-{$pcount}-{$topic_id}-{$uid}";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			$p = intval($p);
			$pcount = intval($pcount);
			
			$status = mysql_escape_string($status);
			$topic_id = intval($topic_id);
			$show_type = intval($show_type);
			$uid = intval($uid);
			
			
			$where_ary = array();
			if($uid)$where_ary[] = "create_uid = $uid";
			if($topic_id)$where_ary[] = "topic_id = $topic_id";
			$where_ary[] = "status = 1";
			
			$where = join (" and ",$where_ary);
			
			$content = $this->get_alllist($this->table_name,$p,$pcount,"create_time desc",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
	
		return $content;
		
	}
	function get_mblog_video_count_by_topic($topic_id="",$uid="")
	{
	
		$mc = mem_cache::get_instance();
	
		$key = CACHE_PREFIX_video."-get_mblog_video_count_by_topic-{$topic_id}-{$uid}";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			$status = mysql_escape_string($status);
			$topic_id = intval($topic_id);
			$show_type = intval($show_type);
			$uid = intval($uid);
			
			$where_ary = array();
			if($uid)$where_ary[] = "create_uid = $uid";
			if($topic_id)$where_ary[] = "topic_id = $topic_id";
			$where_ary[] = "status = 1";
			$where = join (" and ",$where_ary);
			
			$content = $this->get_listcount($this->table_name,$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
	
		
	}
	
	
	
	function get_mblog_video_list_biger_by_topic_and_create_time($p=1,$pcount=10,$topic_id="",$create_time="")
	{
		$mc = mem_cache::get_instance();
	
		$key = CACHE_PREFIX_PUBU."-get_mblog_pubu_list_biger_by_topic_and_id-{$p}-{$pcount}-{$topic_id}-{$create_time}";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			$pcount = intval($pcount);
			$p = intval($p);
			
			$create_time = mysql_escape_string($create_time);
			$topic_id = intval($topic_id);
			
			
			$where_ary = array();
			if($create_time)$where_ary[] = " create_time >= '{$create_time}'";
			if($topic_id)$where_ary[] = "topic_id = $topic_id";
			$where_ary[] = "status = 1";
			$where = join (" and ",$where_ary);
			
			$content = $this->get_alllist($this->table_name,$p,$pcount,"create_time",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
		
	}
	
	function get_mblog_video_list_smaller_by_topic_and_create_time($p=1,$pcount=10,$topic_id="",$create_time="")
	{
		$mc = mem_cache::get_instance();
	
		$key = CACHE_PREFIX_PUBU."-get_mblog_pubu_list_smaller_by_topic_and_create_time-{$p}-{$pcount}-{$topic_id}-{$create_time}";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			$pcount = intval($pcount);
			$p = intval($p);
			
			$create_time = mysql_escape_string($create_time);
			$topic_id = intval($topic_id);
			
			$where_ary = array();
			if($create_time)$where_ary[] = " create_time <= '{$create_time}'";
			if($topic_id)$where_ary[] = "topic_id = $topic_id";
			$where_ary[] = "status = 1";
			$where = join (" and ",$where_ary);
			
			$content = $this->get_alllist($this->table_name,$p,$pcount,"create_time desc",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
		
	}
	
	
	
	function get_mblog_video_list_by_ids($ids)
	{
		$mc = mem_cache::get_instance();
		
		if(is_array($ids))
		{
			$ids = array_unique($ids);
			$ids = join(",",$ids);
			
		}
		if(!$ids)
		{
			return false;
			
		}
		$key = CACHE_PREFIX_video."-get_mblog_video_list_by_ids-$ids";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			$ids = mysql_escape_string($ids);
			
			
			$where_ary = array();
			$where_ary[] = "id in ( {$ids} )";
			$where_ary[] = "status = 1";
			$where = join (" and ",$where_ary);
			
			$content = $this->get_alllist($this->table_name,$p,$pcount,"",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
		
	}
	
	
	
	function get_mblog_video_list_order_by_count_tuijian($p=1,$pcount=10,$topic_id="",$status="",$show_type="",$uid="")
	{
		$mc = mem_cache::get_instance();
	
		$key = CACHE_PREFIX_video."-get_mblog_video_list_order_by_count_tuijian-{$p}-{$pcount}-{$topic_id}-{$status}-{$uid}-{$show_type}";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			
			$p = intval($p);
			$pcount = intval($pcount);
			
			$status = mysql_escape_string($status);
			$topic_id = intval($topic_id);
			$show_type = intval($show_type);
			$uid = intval($uid);
			
			
			$where_ary = array();
			if($uid)$where_ary[] = "create_uid = $uid";
			if($topic_id)$where_ary[] = "topic_id = $topic_id";
			$where_ary[] = "status = 1";
			if($show_type)$where_ary[] = "show_type = $show_type";
			$where = join (" and ",$where_ary);
			
			$content = $this->get_alllist($this->table_name,$p,$pcount,"update_date desc ",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
		
	}
	
	

	function insert_mblog_video_content($content,$topic_id,$topic,$pic,$pic_width_video,$pic_height_video,$source,$show_type,$from,$uid,$nickname)
	{
		if(!$topic) $topic= "";
		$ary["content"] = $content;
		
		$now = time();
		$now = $now - ($now %3);
		$md5_key = $uid.$now;//内容保持唯一
		$md5_key =  md5($md5_key);
		
		$ary["md5_key"] = $md5_key;
		$ary["topic_id"] = $topic_id;
		$ary["topic"] = $topic;
		
		$ary["pic"] = $pic;
		$ary["pic_width_video"] = $pic_width_video;
		$ary["pic_height_video"] = $pic_height_video;
		$ary["source"] = $source;
		$ary["show_type"] = $show_type;
		$ary["from"] = $from;
		
		
		
		
		$ary["status"] = 1;
		$ary["create_uid"] = $uid;
		$ary["create_nickname"] = $nickname;
		$ary["create_time"] = Date::get_date_time();
		$ary["create_ip"] = IP::get_client_ip_long();
		$ary["update_date"] = Date::get_date();
		
		$ret = $this->insert($this->table_name, $ary );
		
		return $this->get_lastinsertid();
		
	}

	
	function update_video_info_by_id($id,$status="")
	{
		$id = intval($id);
		$where = "id = $id";
		$ary = array();

		
		
		if(strlen($status) > 0 )
		{
			$ary["status"] = $status;
			
		}
		
		$ret = $this->update($this->table_name, $ary,$where);
		
		return $ret;
	}
	
###########################

	
		/**
	 * 用户注册
	 * @param $user_name
	 * @param $password
	 */
	function admin_insert_mblog_video_content_by_cron_id($id)
	{
		// 插入数据
		$ary = array();
		$from = "topic_mblog";
		
		
		
		$sql ="insert ignore into {$this->table_name} (show_type,`from`,mid,content,md5_content,topic_id,topic,pic,pic_width_video,pic_height_video,split_word,source,location,coordinates,create_uid,create_nickname,create_time,create_ip,status,count_love) select show_type,'1',id,content,md5(content),topic_id,topic,pic,pic_width_video,pic_height_video,split_word,source,location,coordinates,create_uid,create_nickname,create_time,create_ip,status,count_love from mblog_cron_content where id =$id";
		
		$ret = $this->query($sql);
	
		return $ret;
	}
	
	
	/**
	 * cron 获取food_tag
	 */
	function admin_get_count_by_search($status="",$min_height="",$max_height="",$topic="",$show_type="")
	{
		$where= array();
		if(strlen($status) > 0)
		{
			$status = intval($status);
			$where[] = "status = {$status}";
		}
		
		if($min_height) 
		{
			$where[] = "pic_height_video > '{$min_height}'";
		}
		if($max_height)
		{
			$where[] = "pic_height_video < '{$max_height}'";
		}
		if($topic)
		{
			$where[] = "topic = '{$topic}'";
		}
		if($show_type)
		{
			$where[] = "show_type = '{$show_type}'";
		}
		
		$where= join(" and ",$where);
		
		$data = $this->get_listcount($this->table_name, $where);
		
		return $data;
	}
	
	
	/*
	 * cron获取topic_lisy
	 */
	function admin_get_list_by_search($status="",$p=1,$pcount=10,$min_height="",$max_height="",$topic="",$show_type="")
	{
		$where= array();
		if(strlen($status) > 0)
		{
			$status = intval($status);
			$where[] = "status = {$status}";
		}
		if($min_height) 
		{
			$where[] = "pic_height_video > '{$min_height}'";
		}
		if($max_height)
		{
			$where[] = "pic_height_video < '{$max_height}'";
		}
		if($topic)
		{
			$where[] = "topic = '{$topic}'";
		}
	if($show_type)
		{
			$where[] = "show_type = '{$show_type}'";
		}
		$where= join(" and ",$where);
		
		$p = intval($p);
		$pcount = intval($pcount);
		
		
		$data = $this->get_alllist($this->table_name,$p,$pcount,"create_time desc",$where);
		
		return $data;
	}
	
		
	function admin_update_mblog_info_by_id($id,$status="")
	{
		$where = "id = $id";
		$ary = array();

		if(strlen($status)>0)$ary["status"] = $status;
		$ret = $this->update($this->table_name, $ary,$where);
		
		return $ret;
	}
	
	
function cron_get_data_list_width_wrong($p,$pcount)
	{
		
		$where = "pic_height_video = 0 and pic!='' and create_ip != -1";
		$data = $this->get_alllist($this->table_name,$p,$pcount,"",$where);
		return $data;
	}
	
	
#######################	
	function cron_get_data_count_width_wrong()
	{
		
		$where = "pic_height_video = 0 and pic!='' and create_ip != -1";
		$data = $this->get_listcount($this->table_name,$where);
		return $data;
	}
	
	function cron_update_mblog_info_by_id($id,$pic_width_video="",$pic_height_video="",$status="")
	{
		$where = "id = $id";
		$ary = array();

		if(strlen($pic_width_video)>0)$ary["pic_width_video"] = $pic_width_video;
		if(strlen($pic_height_video)>0)$ary["pic_height_video"] = $pic_height_video;
		if(strlen($status)>0)$ary["status"] = $status;
		$ret = $this->update($this->table_name, $ary,$where);
		
		return $ret;
	}
	
  	function get_user_order_by_count_tuijian_nocache($show_type,$p,$pcount)
	{
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		$topic_list = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_list_order_by_like($show_type,1,10);
		
		
		$content = array();
		foreach($topic_list as $value)
		{
			$topic_id = $value["id"];
			$sql ="select * from mblog_video_content where topic_id = $topic_id order by  update_date desc , count_tuijian desc limit 1";
			
			$res = $db_mblog_cron_topic_tag->select($sql);
			
			$res = $res[0];
			$content[] = $res;
		}
		
		return $content ;
	}
	
	
	
	function cron_insert_mblog_video_content($content,$topic_id,$topic,$pic,$pic_width_video,$pic_height_video,$source,$show_type,$from,
	$uid,$nickname,$update_date,$mid,$create_time,$count_love,$head_url)
	{
		
		$check_have = $this->cron_get_list_by_mid($mid);
		if($check_have)
		{
			echo "have had";
			if($check_have["count_love"]  <  $count_love)
			{
				$this->cron_update_count_love_by_mid($mid,$count_love)	;
			}	
			return $check_have["id"];
		}
		
		$content = preg_replace("/\<.*?\>/is","",$content);
		preg_match_all("/(?<![>|\"|'])\s*(http:[A-Za-z0-9_\/\.]*)/",$content,$matchs);
		$video_pic = "";
		$video_url ="";
		$video_link= "";
		$video_title= "";
		if($matchs[1])
		{
				$mblog_from_api = new mblog_from_api();
				$res = $mblog_from_api->api_short_url_info(join(",",$matchs[1]),990);
				
				
				foreach($res["data"]["result"] as $value)
				{
					if($value["annotations"][0]["pic"])
					{
						$video_url = $value["annotations"][0]["url"];
						$video_pic= $value["annotations"][0]["pic"];
						$video_link = $value["url_long"];
						$video_title  = $value["annotations"][0]["title"];
						break;
					}
				}
		}
		else
		{
			echo "no match link\n";
			return false;
		}	
		
		if(!$video_link || !$video_pic)
		{
			echo "get no ppic or link \n";
			return false;
		}
		
		
		$ary["content"] = $content;
		$ary["count_love"] = $count_love;
		$ary["topic_id"] = $topic_id;
		$ary["topic"] = $topic;
		$ary["pic"] = $pic;
		$ary["video_pic"] = $video_pic;
		$ary["video_url"] = $video_url;
		$ary["video_link"] = $video_link;
		$ary["video_title"] = $video_title;
		$ary["source"] = $source;
		$ary["show_type"] = $show_type;
		$ary["from"] = $from;
		$ary["mid"] = $mid;
		$ary["head_url"] = $head_url;
		
		
		$ary["status"] = 1;
		$ary["create_uid"] = $uid;
		$ary["create_nickname"] = $nickname;
		$ary["create_time"] = $create_time;
		$ary["create_ip"] = 0;
		
		
		
		$update_date = strtotime($create_time);
		if($count_love)$update_date = log($count_love)*86400 + $update_date;
		$ary["update_date"] = $update_date;
		
		echo "do insert ;\n";
		$ret = $this->insert($this->table_name, $ary );
		
		return $this->get_lastinsertid();
		
	}
	
	
	
	
	function cron_qq_insert_mblog_video_content($content,$topic_id,$topic,$pic,$pic_width_video,$pic_height_video,$source,$show_type,$from,
	$uid,$nickname,$update_date,$mid,$create_time,$count_love,$head_url,$video_pic,$video_url,$video_link,$video_title)
	{
		
		$check_have = $this->cron_get_list_by_mid($mid);
		if($check_have)
		{
			echo "have had";
			if($check_have["count_love"]  <  $count_love)
			{
				$this->cron_update_count_love_by_mid($mid,$count_love)	;
			}	
			return $check_have["id"];
		}
		
		
		
		$ary["content"] = $content;
		$ary["count_love"] = $count_love;
		$ary["topic_id"] = $topic_id;
		$ary["topic"] = $topic;
		$ary["pic"] = $pic;
		$ary["video_pic"] = $video_pic;
		$ary["video_url"] = $video_url;
		$ary["video_link"] = $video_link;
		$ary["video_title"] = $video_title;
		$ary["source"] = $source;
		$ary["show_type"] = $show_type;
		$ary["from"] = $from;
		$ary["mid"] = $mid;
		$ary["head_url"] = $head_url;
		
		
		$ary["status"] = 1;
		$ary["create_uid"] = $uid;
		$ary["create_nickname"] = $nickname;
		$ary["create_time"] = $create_time;
		$ary["create_ip"] = 0;
		
		
		
		$update_date = strtotime($create_time);
		if($count_love)$update_date = log($count_love)*86400 + $update_date;
		$ary["update_date"] = $update_date;
		
		echo "do insert ;\n";
		$ret = $this->insert($this->table_name, $ary );
		
		return $this->get_lastinsertid();
		
	}
	
	
	
	//抓取用户最近的微博
	function cron_get_mblog_list_order_by_create_time($uid,$p=1,$pcount=10)
	{
		$where = "create_uid = {$uid} and status = 1";
		$list = $this->get_alllist($this->table_name,$p,$pcount,"",$where)	;
		return $list;
	}
	
	function cron_get_list_by_mid($mid)
	{
	
		$where = "mid = $mid";
		
		$res = $this->get_alllist($this->table_name,1,1,"", $where);
		return $res[0];
		
		
	}
	
	function cron_update_count_love_by_mid($mid,$count_love)
	{
		$where = "mid = $mid";
		$ary["count_love"] = $count_love;
		
		$res= $this->update($this->table_name,$ary,$where);
		
		return $res;
 	}
	function cron_get_list_for_reply_from_sina($p,$pcount,$id)
	{
		$where = "id > $id and status = 1 and `from`=2";
		$data = $this->get_alllist($this->table_name,$p,$pcount,"id",$where);
	
		return $data;
	}
	
	function cron_update_mblog_count_reply($id)
	{
		$db_mblog_video_reply = new db_mblog_video_reply();
		
		$count = $db_mblog_video_reply->cron_get_count_by_mid($id);
		
		$sql = "update {$this->table_name} set count_reply = $count where id = $id ";
		$this->query($sql);
		
		
	}
}



